<#assign ctx="${rc.contextPath}">
<html>
<head>
    <title>角色管理</title>
    <script type="text/javascript">
        $(document).ready(function () {
            var winH = $(window).height();
            var toolH = $(".toolbar").outerHeight();
            var crumbH = $(".breadcrumb").outerHeight();
            var tgH = (winH - toolH - crumbH)*0.7;

            $('#tg').treegrid({
                rownumbers: false,
                url: '${ctx}/account/role/list',
                height: tgH,
                idField: 'accRoleId',
                fitColumns: true,
                singleSelect: true
            });

            $('#dlg').dialog({
                title: "查看权限",
                width: 300,
                height: 500,
                iconCls: 'icon icon-search',
                modal: true,
                closed: true,
                buttons: [{
                    text: '授权', handler: function () {
                        grant();
                    }
                }, {
                    text: '取消', handler: function (e) {
                        $('#dlg').dialog('close');
                    }
                }]
            });
        });

        var editingId;
        function add() {
            if (editingId != undefined) {
                $.messager.alert('提示', "当前有正在编辑的节点!", "warning");
                return;
            }
            var idIndex = uuid();
            $('#tg').treegrid('append', {
                data: [{
                    accRoleId: idIndex,
                    accRoleName: '',
                    accRoleKey: ''
                }]
            });
            editingId = idIndex;
            $('#tg').treegrid('select', idIndex);
            $('#tg').treegrid('beginEdit', idIndex);
        }

        function edit() {
            if (editingId != undefined) {
                $('#tg').treegrid('select', editingId);
                return;
            }
            var row = $('#tg').treegrid('getSelected');
            if (undefined == row || null == row) {
                $.messager.alert('提示', "没有选择需要编辑的节点!", "warning");
                return;
            }
            editingId = row.accRoleId;
            $('#tg').treegrid('beginEdit', editingId);
        }

        function save() {
            if (editingId != undefined) {
                $.messager.confirm('确认', '是否保存角色?', function (r) {
                    if (r) {
                        var t = $('#tg');
                        t.datagrid('endEdit', editingId);
                        $('#tg').treegrid('select', editingId);
                        editingId = undefined;
                        var row = t.datagrid('getSelected');
                        $.ajax({
                            type: "POST",
                            url: "${ctx}/account/role/save",
                            data: row,
                            dataType: "json",
                            success: function (result) {
                                successMsg(result.message);
                                $('#tg').treegrid("reload");
                            }
                        });
                    }
                });
            }
        }

        function cancel() {
            if (editingId != undefined) {
                $('#tg').treegrid('cancelEdit', editingId);
                editingId = undefined;
                $('#tg').treegrid("reload");
            }
        }

        function del() {
            var row = $('#tg').treegrid('getSelected');
            if (undefined == row || null == row) {
                $.messager.alert('提示', "没有选择需要删除的节点!", "warning");
                return;
            }
            if (editingId != undefined) {
                $.messager.alert('提示', "当前有正在编辑的节点!", "warning");
                return;
            }
            if (row.accPermissionParentId == "") {
                $.messager.alert('提示', "不能删除根节点!", "warning");
                return;
            }
            $.messager.confirm('确认', '是否删除角色?', function (r) {
                if (r) {
                    $.ajax({
                        type: "POST",
                        url: "${ctx}/account/role/del",
                        data: {accRoleId: row.accRoleId},
                        dataType: "json",
                        success: function (result) {
                            successMsg(result.message);
                            $('#tg').treegrid("reload");
                        }
                    });
                }
            });
        }

        function showPermission() {
            var row = $('#tg').treegrid('getSelected');
            if (row) {
                $('#dlg').dialog('open');
                $('#tt').tree({
                    url: '${ctx}/account/role/permissions?accRoleId=' + row.accRoleId,
                    animate: true,
                    checkbox: true
                });
            }
        }

        function grant() {
            var checked = $("#tt").tree('getChecked');
            var arr = [];
            $(checked).each(function (i, v) {
                arr[i] = v.id;
            });
            var row = $('#tg').treegrid('getSelected');
            $.ajax({
                type: "post",
                url: "${ctx}/account/role/grant",
                data: {accRoleId: row.accRoleId, perIdArray: arr},
                dataType: "json",
                success: function (result) {
                    successMsg(result.message);
                    $('#dlg').dialog('close');
                }
            })
        }

    </script>
</head>
<body>
<div class="breadcrumb"> 系统管理 > 角色管理</div>

<div id="toolbar" class="toolbar">
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-add" onclick="add();"
       plain="true">添加角色</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-edit" onclick="edit();"
       plain="true">编辑</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-save" onclick="save();"
       plain="true">保存</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-cancel" onclick="cancel();"
       plain="true">取消</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-remove" onclick="del();"
       plain="true">删除</a>
    <a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-search" onclick="showPermission()"
       plain="true">查看权限</a>
</div>

<table id="tg">
    <thead>
    <tr>
        <th data-options="field:'accRoleId', width:20,hidden:true "></th>
        <th data-options="field:'accRoleName', width:100,editor:'text'">角色名称</th>
        <th data-options="field:'accRoleKey', width:100,editor:'text'">角色键值</th>
    </tr>
    </thead>
</table>

<div id="dlg">
    <ul id="tt"></ul>
</div>

</body>
</html>